package com.ddtech.business.course.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.constant.Del;
import com.ddtech.business.course.entity.CourseChapter;
import com.ddtech.business.course.entity.CourseResource;
import com.ddtech.business.course.mapper.CourseResourceMapper;
import com.ddtech.business.course.service.CourseResourceService;
import com.ddtech.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bin
 */
@Service
public class CourseResourceServiceImpl extends ServiceImpl<CourseResourceMapper, CourseResource> implements CourseResourceService {
    @Override
    public void saveResource(CourseChapter chapter) {
        LambdaQueryWrapper<CourseResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseResource::getChapterId, chapter.getId());
        List<CourseResource> courseResourceListInfo = this.list(queryWrapper);
        List<CourseResource> courseResourceList = chapter.getCourseResourceList();
        //courseResourceListInfo-courseResourceList
        List<CourseResource> collect = courseResourceListInfo.stream().filter(item -> !courseResourceList.contains(item)).collect(Collectors.toList());
        for (CourseResource courseResource : collect) {
            courseResource.baseDelete();
            this.saveOrUpdate(courseResource);
        }
        courseResourceList.forEach(resource -> {
            resource.setChapterId(chapter.getId());
            resource.init();
            this.saveOrUpdate(resource);
        });
    }

    @Override
    public List<CourseResource> getResource(String chapterId) {
        LambdaQueryWrapper wrapper = Wrappers.<CourseResource>lambdaQuery()
                .eq(CourseResource::getChapterId, chapterId)
                .eq(CourseResource::getIsDelete, Del.FALSE.getStatus())
                .orderByAsc(CourseResource::getSort);
        return this.list(wrapper);
    }

    @Override
    public List<CourseResource> getResourceList(String chapterId) {
        LambdaQueryWrapper wrapper = Wrappers.<CourseResource>lambdaQuery()
                .eq(CourseResource::getChapterId, chapterId)
                .eq(CourseResource::getIsDelete, Del.FALSE.getStatus())
                .orderByAsc(CourseResource::getSort);
        List<CourseResource> courseResourceList =this.list(wrapper);
//        for(CourseResource courseResource:courseResourceList){
//            String file=courseResource.getResourceFile();
//            if(StringUtils.isNotEmpty(file)){
//                JSONArray array = JSON.parseArray(file);
//                if (array!=null){
//                    JSONObject json =array.getJSONObject(0);
//                    courseResource.setResourceFile(json.getString("url"));
//                }
//            }
//        }
        return courseResourceList;
    }

    @Override
    public void removeResource(CourseChapter courseChapter) {
        LambdaQueryWrapper<CourseResource> removeQuery = new LambdaQueryWrapper<>();
        removeQuery.eq(CourseResource::getChapterId, courseChapter.getId());
        List<CourseResource> courseResourceList = this.list(removeQuery);
        if (CollUtil.isNotEmpty(courseResourceList)) {
            courseResourceList.forEach(item -> {
                item.baseDelete();
                this.saveOrUpdate(item);
            });
        }
    }

    /***
     * description: 查询课程播放资源url
     * @param:  * @param id
     * @date: 2023-06-09
     * @return: java.lang.Object
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public CourseResource findOne(String id) {
        CourseResource courseResource=this.getById(id);
        if(courseResource!=null){
            String obj=courseResource.getResourceFile();
            if(StringUtils.isNotEmpty(obj)){
                JSONArray array = JSON.parseArray(obj);
                if (array!=null && array.size()>0){
                    JSONObject json=array.getJSONObject(0);
                    courseResource.setResourceFile(json.getString("url"));
                }
            }
        }
        return courseResource;
    }
}
